<template src="./template.html"></template>
<script>
/* global moment */
import checkForm from 'mixins/checkform'
import wangEditor from 'components/wang-editor'
import { updateCourse } from '../models'
import CourseMixins from '../mixins'
import { validateCourseName, validateRichText } from 'utils/ui-utils'

export default {
  inject: ['getCourseInfo'],
  components: {
    wangEditor
  },
  data() {
    return {
      isPageLoading: false,
      basicForm: {
        courseName: '',
        courseIntroduction: '',
        deadline: ''
      },
      rules: {
        courseName: [{ required: true, message: '请输入课程名称', trigger: 'change' },
          { validator: validateCourseName, trigger: 'blur' }],
        courseIntroduction: [{ validator: validateRichText, trigger: 'change' }]
      },
      key: Math.random()
    }
  },
  mixins: [
    checkForm,
    CourseMixins
  ],
  watch: {
    '$store.state.pageStatusInfo': {
      handler() {
        const cache = this.$store.state.pageStatusInfo.courseLayoutInfoData
        if (cache) {
          for (const k in this.basicForm) {
            this.basicForm[k] = cache[k]
          }
          console.log('basicForm:', this.basicForm)
        }
      },

      deep: true,
      immediate: true,
    }
  },
  methods: {
    async $saveForm() {
      if (!this.$checkForm('basicForm')) {
        return
      }
      this.isPageLoading = true
      const params = {
        ...this.basicForm,
        id: this.courseId,
        deadline: this.basicForm.deadline ? moment(this.basicForm.deadline).format('YYYY-MM-DD HH:mm:ss') : ''
      }
      try {
        await updateCourse(params)
        this.isPageLoading = false
        this.$refs['basicForm'].resetFields()
        this.$message.success('保存信息成功')
        this.getCourseInfo()
      } catch (err) {
        this.isPageLoading = false
        this.$message.error(err.message || err.message || '保存信息出错')
      }
    },
    // async getById() {
    //   const params = {
    //     id: this.$route.params.id
    //   }
    //   this.isPageLoading = true
    //   try {
    //     const res = await getCustomerById(params)
    //     const data = res.data || {}
    //     for (const k in this.basicForm) {
    //       if (data[k] === null || data[k] === undefined) {
    //         this.basicForm[k] = ''
    //       } else {
    //         this.basicForm[k] = data[k]
    //       }
    //     }
    //     if (data.funIds) {
    //       this.basicForm.funIds = data.funIds.split(',')
    //     } else {
    //       this.basicForm.funIds = []
    //     }
    //     if (data.authCount === 0) {
    //       this.basicForm.isLimitNums = '0'
    //       this.basicForm.authCount = ''
    //     } else {
    //       this.basicForm.isLimitNums = '1'
    //     }
    //     this.isPageLoading = false
    //   } catch (err) {
    //     console.log(err)
    //     this.isPageLoading = false
    //     this.$message.error(err.message || err.message || '拉取机构详细异常')
    //   }

    // },
  },
  mounted() {
  },
};
</script>
